package com.matt.controller.web.care;

import com.matt.controller.pad.care.CareBaseController;
import com.matt.domain.care.HLDailyRecord;
import com.matt.domain.care.HLMoreRecord;
import com.matt.foundation.vo.care.HLDailyRecordVO;
import com.matt.foundation.vo.care.HLDailyReportVO;
import com.matt.service.care.*;
import com.matt.service.care.po.HLDailyRecordPO;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import com.starinside.wp.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/web/record/daily")
@Slf4j
@Api(tags = "护理记录 - 宝宝一日")
public class WebCareDailyController extends CareBaseController<HLDailyRecord, HLDailyRecordService, HLDailyRecordPO> {

    @Autowired
    HLSleepRecordService sleepRecordService;

    @Autowired
    HLFeedingRecordService feedingRecordService;

    @Autowired
    HLPottyRecordService pottyRecordService;

    @Autowired
    HLMedicationRecordService medicationRecordService;

    @Autowired
    HLMoreRecordService hlMoreRecordService;

    @Autowired
    public void setBaseService(HLDailyRecordService service){
        super.setBaseService(service);
    }

    @ApiOperation(value = "获取一个简报对象")
    @GetMapping("/report")
    public StatusResponse getDailyReport(@RequestHeader(TICKET) String ticket, HttpServletRequest req){
        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

        BindedBabyVO baby = loginUser.getDefaultBaby();

        HLDailyReportVO reportVO = new HLDailyReportVO();

        reportVO.setChildId(baby.getId());
        reportVO.setFeedRank(3);
        reportVO.setStudyRank(4);
        reportVO.setHealthRank(5);
        reportVO.setSummary("小朋友今天表现优秀，在健康方面一切正常，在饮食方面也摄入了丰富的营养。同时，今天也在园区学习了丰富的课程!");

        return StatusResponse.success(reportVO);
    }

    @Override
    @ApiOperation(value = "获取一个对象")
    @GetMapping()
    public StatusResponse get(@RequestHeader(TICKET) String ticket, HttpServletRequest req, String childId, String id){
        HLDailyRecord dailyRecord = baseService.get(null, null, childId, id);

        HLDailyRecordVO ret = HLDailyRecordVO.of(dailyRecord);

        Date dailyDate =  DateUtil.parseToDate(dailyRecord.getDaily());
        ret.setSleepRecords(sleepRecordService.dailyRecords(null, null, childId, dailyDate));
        ret.setPottyRecords(pottyRecordService.dailyRecords(null, null, childId, dailyDate));
        ret.setFeedingRecords(feedingRecordService.dailyRecords(null, null, childId, dailyDate));
        ret.setMedicationRecords(medicationRecordService.dailyRecords(null, null, childId, dailyDate));
        ret.setSleepRecords(sleepRecordService.dailyRecords(null, null, childId, dailyDate));
        ret.setHlMoreRecords(hlMoreRecordService.dailyRecords(null, null, childId, dailyDate));

        return StatusResponse.success(dailyRecord);
    }

    @ApiOperation(value = "获取列表")
    @GetMapping("/list")
    @Override
    public StatusResponse getList(@RequestHeader(TICKET) String ticket, HttpServletRequest req, HttpServletResponse resp,
                                  String childId, PagerQuery pagerQuery){
        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());

        return StatusResponse.success( baseService.page(null, null, childId, pageRequest));
    }

}
